package com.slz3.business.module.memberManagement.memJifen.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.ConvertUtil;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.memberManagement.memJifen.domain.MemJifen;
import com.slz3.business.module.memberManagement.memJifen.domain.MemJifenFactory;
import com.slz3.business.module.memberManagement.memMember.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class MemJifenResExecutor {

    static Mono<ServerResponse> add(ServerRequest req, MemJifen memJifen) {
        MemJifen memJifenDBLatest = Services.memJifen.save(memJifen);
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(memJifenDBLatest).build());
    }

    /**
     * known: memJifenList is not null
     */
    public static Mono<ServerResponse> delete(ServerRequest req) {
        List<MemJifen> memJifenList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(memJifenList)) {
            List<MemJifen> memJifenListNew = Services.memJifen.delete(memJifenList);
            if (VE.isNotEmpty(memJifenListNew) && memJifenListNew.size() == memJifenList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(memJifenListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> modify(ServerRequest req, MemJifen source) {
        MemJifen target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            MemJifen memJifenDBLatest = Services.memJifen.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(memJifenDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * 获取用户积分记录
     * @param req
     * @return
     */
    static Mono<ServerResponse> list(ServerRequest req) {
        /**
         * 获取用户积分记录
         */
        Page<MemJifen> memJifenPage = Services.memJifen.page(req.queryParams());
        List<MemJifen> userScoreRecords = memJifenPage.getContent();
        if(userScoreRecords != null){
            /**
             *  获取主键ID
             */
            List<String> keyList = new ArrayList<>();
            for (MemJifen vo: userScoreRecords) {
                keyList.add(RK.USER_INFO.concat(":" + vo.getSerJifenUserId()));
            }
            List<Object> listByKey = Jedis.db0.getListByKey(keyList);
            // 获取用户信息
            List<User> users = ConvertUtil.objToList(listByKey, User.class);
            // 增加用户姓名
            for (MemJifen vo: userScoreRecords) {
                Optional<User> optionalUser = users.stream().filter(u -> u.getMemberId().equals(vo.getSerJifenUserId().toString())).findFirst();
                if(optionalUser.isPresent()){
                    User user = optionalUser.get();
                    String serUserName = user.getUserName();//用户昵称
                    String serAccountNo = user.getAccountNo();// 用户账户
                    vo.setSerUserName(serUserName);
                    vo.setSerAccountNo(serAccountNo);
                }
            }
        }
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(memJifenPage).build());
    }

    static Mono<ServerResponse> info(ServerRequest req) {
        MemJifen target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> choice(ServerRequest req) {
        Page<MemJifen> memJifenPage = Services.memJifen.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(memJifenPage).build());
    }

    /**
     * known: memJifenList is not null
     */
    public static Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<MemJifen> memJifenList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(memJifenList)) {
            memJifenList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.WAIT_AUDIT);
                MemJifenFactory.initBaseFields(item);
            });
            List<MemJifen> memJifenListNew = Services.memJifenTransactional.batchModify(memJifenList);
            if (VE.isNotEmpty(memJifenListNew) && memJifenListNew.size() == memJifenList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(memJifenListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: memJifenList is not null
     */
    public static Mono<ServerResponse> audit(ServerRequest req) {
        List<MemJifen> memJifenList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(memJifenList)) {
            memJifenList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.APPROVED);
                MemJifenFactory.initBaseFields(item);
            });
            List<MemJifen> memJifenListNew = Services.memJifenTransactional.batchModify(memJifenList);
            if (VE.isNotEmpty(memJifenListNew) && memJifenListNew.size() == memJifenList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(memJifenListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: memJifenList is not null
     */
    public static Mono<ServerResponse> auditReject(ServerRequest req) {
        List<MemJifen> memJifenList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(memJifenList)) {
            memJifenList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.REJECT);
                MemJifenFactory.initBaseFields(item);
            });
            List<MemJifen> memJifenListNew = Services.memJifenTransactional.batchModify(memJifenList);
            if (VE.isNotEmpty(memJifenListNew) && memJifenListNew.size() == memJifenList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(memJifenListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: memJifenList is not null
     */
    public static Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<MemJifen> memJifenList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(memJifenList)) {
            memJifenList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.ENABLE);
                MemJifenFactory.initBaseFields(item);
            });
            List<MemJifen> memJifenListNew = Services.memJifenTransactional.batchModify(memJifenList);
            if (VE.isNotEmpty(memJifenListNew) && memJifenListNew.size() == memJifenList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(memJifenListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: memJifenList is not null
     */
    public static Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<MemJifen> memJifenList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(memJifenList)) {
            memJifenList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.DISABLE);
                MemJifenFactory.initBaseFields(item);
            });
            List<MemJifen> memJifenListNew = Services.memJifenTransactional.batchModify(memJifenList);
            if (VE.isNotEmpty(memJifenListNew) && memJifenListNew.size() == memJifenList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(memJifenListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

}
